package voxToolkit;

import grafico.Fabrica;

import java.awt.AWTEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;

import main.Contexto;

import audio.AudioSintetizado;
import audio.SoundEvent;
import audio.SoundListener;

public class VoxButtonEvent implements FocusListener, MouseListener, SoundListener{

	private AudioSintetizado sintetizador;
	VoxContexto voxContexto = VoxContexto.instancia();
	static private VoxButtonEvent instancia = null;

	private VoxButtonEvent()
	{

		sintetizador = AudioSintetizado.instancia();
		sintetizador.addSoundListener(this);
	}

	public static VoxButtonEvent instancia ()
	{
		if (instancia == null)
			instancia = new VoxButtonEvent();

		return instancia;
	}
	
	public void tocarLabel(String nome) throws IOException
	{
		voxContexto.execSint(voxContexto.getNomeArqAudio(nome));
	}
	
	public void falaBotao(VoxButton btn){
		
		sintetizador.abortaFala();
		sintetizador.escreve(btn.getRotulo());
	}
	
	
	@Override
	public void focusGained(FocusEvent e){
		
		VoxButton btn = (VoxButton)e.getSource();
		
		String caminhoCompleto = voxContexto.getNomeArqAudio(btn.getNome());
		
		File arquivoBtn = new File(caminhoCompleto);
		
		if (arquivoBtn.exists()){
			try
			{
				tocarLabel(btn.getNome());
				btn.setBorder(Fabrica.bordaClara);
				
			}
			catch (Exception e1)
			{
				e1.printStackTrace();
			}
		}
		
		else{
		
			falaBotao(btn);
		    btn.setBorder(Fabrica.bordaClara);	
		}
	}
		

	@Override
	public void focusLost(FocusEvent e) {
		
		VoxButton btn = (VoxButton)e.getSource();
		
		btn.setBorder(Fabrica.bordaEscura);
		
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		
		VoxButton btn = (VoxButton)e.getSource();
		
		btn.requestFocus();
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void FimDeFala(SoundEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eventDispatched(AWTEvent e) {
		
		if (e instanceof SoundEvent)
			FimDeFala((SoundEvent) e);
		
	}

	
}	
